import java.beans.beancontext.BeanContext;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Queue;
import java.util.Scanner;

public class App {
    public static void main(String[] args) {

        input("<p>111 <span>222</span> <span>333 <div>444</div> </span> 555</p>");
    }

    static void input(String html) {
        Queue<Node> res = new ArrayDeque<>();

        State state = State.Init;
        State befCharState = State.Init;  // 上一个字符的状态
        int beginIndex = 0;
        int endIndex = 0;
        Node node = new Node();
        for (int i = 0; i < html.length(); i++) {
            endIndex = i;
            befCharState = state;
            // 根据字符转化状态
            state = transform(state, html.charAt(i));
            // 单词读取完成或者报错
            if (state == State.Error) {
                // 文本状态.
                if (befCharState == State.Content) {
                    // TODO stack 出栈转 tree
                    // new Node(Type.Content,html.substring(beginIndex,endIndex));
                } else {
                    // TODO 错误处理
                    try {
                        throw new Exception("error");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }


        }

    }

    static State transform(State state, char currChar) {
        switch (state) {
            case Init:
                if (isLegalCharacter(currChar)) return State.Content;
                if (currChar == '<') return State.Left;
                return State.Error;


            // 单独文字处理
            case Content:
                if (isLegalCharacter(currChar)) return State.Content;
                return State.Error;

            // 结束标签处理
            case Left:
                if (currChar == '/') return State.End_1;
                if (isLegalCharacter(currChar)) return State.Begin_1;
                return State.Error;
            case End_1:
                if (isLegalCharacter(currChar)) return State.End_2;
                return State.Error;
            case End_2:
                if (isLegalCharacter(currChar)) return State.End_2;
                if (currChar == '>') return State.End;
                return State.Error;

            // 开始标签处理
            case Begin_1:
                if (isLegalCharacter(currChar)) return State.Begin_1;
                if (currChar == '>') return State.Begin;
                return State.Error;
        }
        return State.Error;
    }


    static boolean isLegalCharacter(char ch) {
        return ch >= '0' && ch <= '9' || ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z';
    }


}
